﻿// echo partial file
#if FRONTEND
using FrameWork.ModSystem;
using HarmonyLib;
using HarmonyLib.Tools;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Image = UnityEngine.UI.Image;
using static NpcScan.Const;
namespace NpcScan
{
    public class View
    {
        public static int result_scale=100;
        public static bool Attraction=false;
        public bool isShow = false;
        public Dictionary<Model.Input, InputField> InputDic;
        public static Dictionary<Model.Toggle, CToggle> ToggleDic;
        public Dictionary<Model.CharacterInfo, CButton> ButtonDic;

        public Transform ScrollDataContent;
        public Transform ScrollTitleContent;
        public List<Transform> ResultLines;
        public List<List<TextMeshProUGUI>> ResultTMP;
        public CButton ButtonNext;
        public CButton ButtonPrevious;
        public CButton ButtonSearch;
        public CButton ButtonUpdate;
        public TextMeshProUGUI PageCount;
        public VerticalLayoutGroup InputContainer;
        public GameObject ScrollView;

        private int index = 0;
        const float MARGIN=0.01f;
        private Camera mainCamera;
        public GameObject root;
        //private Transform tooltip;
        public CanvasGroup blockClickGroup;
        public void Init()
        {
            InputDic = new Dictionary<Model.Input, InputField>();
            ToggleDic = new Dictionary<Model.Toggle, CToggle>();
            ButtonDic = new Dictionary<Model.CharacterInfo, CButton>();
            mainCamera = Camera.main;
            Canvas canvas = mainCamera.transform.Find("Canvas").GetComponent<Canvas>();
            blockClickGroup = canvas.gameObject.AddComponent<CanvasGroup>();

            root = new GameObject("NPCScan");
            root.transform.SetParent(mainCamera.transform);
            root.layer = LayerMask.NameToLayer("UI");
            Canvas rootCanvas = root.AddComponent<Canvas>();
            rootCanvas.renderMode = RenderMode.ScreenSpaceCamera;
            rootCanvas.worldCamera = mainCamera;
            rootCanvas.overrideSorting = true;
            rootCanvas.sortingLayerName = "UI";
            rootCanvas.sortingOrder = 1001;

            root.AddComponent<GraphicRaycaster>();

            var mask = GameObjectCreationUtils.InstantiateUIElement(rootCanvas.transform, "Image");
            mask.GetComponent<CImage>().color = new Color(0.1f, 0.1f, 0.1f, 0.6f);
            mask.GetComponent<RectTransform>().anchorMin = new Vector2(0, 0);
            mask.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1);
            mask.name = "Mask";

            InputContainer = new GameObject("InputContainer").AddComponent<VerticalLayoutGroup>();
            InputContainer.spacing = 30;
            SetTransform(root.transform, InputContainer.transform);
            InputContainer.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            InputContainer.GetComponent<RectTransform>().anchorMin = new Vector2(0, 1f-MARGIN);
            InputContainer.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1f-MARGIN);
            InputContainer.transform.localScale = new Vector3(0.6f, 0.6f, 1);

            var text = GameObjectCreationUtils.InstantiateUIElement(InputContainer.transform, "Text");
            text.GetComponent<TextMeshProUGUI>().text = "Npc查找器 - 中子灌顶版";
            text.transform.name = "title";
            SetTransform(text.transform);

            //InitTooltip();
            InitFilterContainer(InputContainer);
            InitScrollView();

            LayoutRebuilder.ForceRebuildLayoutImmediate(InputContainer.GetComponent<RectTransform>());
        }

        public void Destroy()
        {
            Object.Destroy(root);
        }

        //private void InitTooltip()
        //{
        //    var canva = new GameObject("test");
        //    canva.transform.SetParent(mainCamera.transform);
        //    canva.layer = LayerMask.NameToLayer("UI");
        //    Canvas canvas = canva.AddComponent<Canvas>();
        //    canvas.renderMode = RenderMode.ScreenSpaceCamera;
        //    canvas.worldCamera = mainCamera;
        //    canvas.overrideSorting = true;
        //    canvas.sortingLayerName = "UI";
        //    canvas.sortingOrder = 1002;

        //    tooltip = new GameObject("Tooltip").AddComponent<ContentSizeFitter>().transform;
        //    ResetTransform(canva.transform, tooltip);

        //    tooltip.gameObject.GetComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
        //    tooltip.gameObject.GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.PreferredSize;

        //    tooltip.gameObject.AddComponent<VerticalLayoutGroup>();

        //    var image = tooltip.gameObject.AddComponent<Image>();

        //    CreateLabel(tooltip.transform, "");

        //    tooltip.gameObject.SetActive(false);
        //}

        #region InitFilter
        private void InitFilterContainer(VerticalLayoutGroup container)
        {
            var filterContainer = new GameObject("FilterContainer").AddComponent<VerticalLayoutGroup>();
            filterContainer.childForceExpandHeight = false;
            filterContainer.gameObject.AddComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
            filterContainer.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            SetTransform(container.transform, filterContainer.transform);

            CreateBasicInfo(filterContainer);
            CreateCombatSkillInfo(filterContainer);
            CreateLifeSkillInfo(filterContainer);
            CreateToggleGroup(filterContainer);
            CreateMultiSearch(filterContainer);
        }

        private void CreateBasicInfo(VerticalLayoutGroup parent)
        {
            var basicInfo = new GameObject("BasicInfo").AddComponent<HorizontalLayoutGroup>();
            SetTransform(parent.transform, basicInfo.transform);
            basicInfo.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            basicInfo.childForceExpandWidth = false;

            CreateLabel(basicInfo.transform, "年龄:");
            CreateInputField(basicInfo.transform, Model.Input.最低年龄);
            CreateLabel(basicInfo.transform, "-");
            CreateInputField(basicInfo.transform, Model.Input.最大年龄);

            CreateLabel(basicInfo.transform, "入邪程度:");
            CreateInputField(basicInfo.transform, Model.Input.入魔值下限);
            CreateLabel(basicInfo.transform, "-");
            CreateInputField(basicInfo.transform, Model.Input.入魔值上限);

            for (index = (int)Model.Input.膂力; index <= (int)Model.Input.轮回次数; ++index)
            {
                CreateLabel(basicInfo.transform, ((Model.Input)index).ToString() + ":");
                CreateInputField(basicInfo.transform, (Model.Input)index);
            }
        }

        private void CreateCombatSkillInfo(VerticalLayoutGroup parent)
        {
            var combatSkillInfo = new GameObject("CombatSkillInfo").AddComponent<HorizontalLayoutGroup>();
            SetTransform(parent.transform, combatSkillInfo.transform);
            combatSkillInfo.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            combatSkillInfo.childForceExpandWidth = false;

            for (index = (int)Model.Input.内功; index <= (int)Model.Input.乐器; ++index)
            {
                CreateLabel(combatSkillInfo.transform, ((Model.Input)index).ToString() + ":");
                CreateInputField(combatSkillInfo.transform, (Model.Input)index);
            }
        }

        private void CreateLifeSkillInfo(VerticalLayoutGroup parent)
        {
            var lifrSkillInfo = new GameObject("LifrSkillInfo").AddComponent<HorizontalLayoutGroup>();
            SetTransform(parent.transform, lifrSkillInfo.transform);
            lifrSkillInfo.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            lifrSkillInfo.childForceExpandWidth = false;

            for (index = (int)Model.Input.音律; index <= (int)Model.Input.杂学; ++index)
            {
                CreateLabel(lifrSkillInfo.transform, ((Model.Input)index).ToString() + ":");
                CreateInputField(lifrSkillInfo.transform, (Model.Input)index);
            }
        }
        void CreateMultiToggleGroup(Transform trans, string label, Model.Toggle start, Model.Toggle end){
            CreateLabel(trans, label);
            for (index = (int)start; index <= (int)end; ++index) {
                CreateToggle(trans, (Model.Toggle)index, index==(int)start?(Model.Toggle)((int)start+1):start, index==(int)start?end:start, ((Model.Toggle)index).ToString());
            }
        }
        void CreateMutexToggleGroup(Transform trans, string label, Model.Toggle start, Model.Toggle end){
            CreateLabel(trans, label);
            for (index = (int)start; index <= (int)end; ++index) {
                CreateToggle(trans, (Model.Toggle)index, index==(int)start?(Model.Toggle)((int)start+1):start, end, ((Model.Toggle)index).ToString());
            }
        }
        private void CreateToggleGroup(VerticalLayoutGroup parent)
        {
            var toggleGroup = new GameObject("ToggleGroup").AddComponent<HorizontalLayoutGroup>();
            SetTransform(parent.transform, toggleGroup.transform);
            toggleGroup.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            toggleGroup.childForceExpandWidth = false;

            CreateMultiToggleGroup(toggleGroup.transform, "婚姻:", Model.Toggle.全部婚姻, Model.Toggle.丧偶);
            CreateMultiToggleGroup(toggleGroup.transform, "立场:", Model.Toggle.全部立场, Model.Toggle.唯我);
            CreateMultiToggleGroup(toggleGroup.transform, "归属:", Model.Toggle.全部归属, Model.Toggle.村庄);
            CreateMultiToggleGroup(toggleGroup.transform, "人身自由:", Model.Toggle.无关紧要, Model.Toggle.被拘禁);

            toggleGroup = new GameObject("ToggleGroup").AddComponent<HorizontalLayoutGroup>();
            SetTransform(parent.transform, toggleGroup.transform);
            toggleGroup.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            toggleGroup.childForceExpandWidth = false;

            CreateMutexToggleGroup(toggleGroup.transform, "性别:", Model.Toggle.全部性别, Model.Toggle.女性);
            CreateMutexToggleGroup(toggleGroup.transform, "生相:", Model.Toggle.全部生相, Model.Toggle.不同);
            CreateMutexToggleGroup(toggleGroup.transform, "取向:", Model.Toggle.全部取向, Model.Toggle.双性);
            CreateMutexToggleGroup(toggleGroup.transform, "搜索范围:", Model.Toggle.全世界, Model.Toggle.同地格);
            CreateMutexToggleGroup(toggleGroup.transform, "生命体征:", Model.Toggle.不论死活, Model.Toggle.健在);

            CreateLabel(toggleGroup.transform, "其他:");
            for(int idx=(int)Model.Toggle.村民灌顶;idx<(int)Model.Toggle.Count;idx++){
                CreateToggle(toggleGroup.transform, (Model.Toggle)idx);
            }
            ToggleDic[Model.Toggle.自动反选].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.村民灌顶].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.同时刷新].GetComponent<CToggle>().isOn = ROW_MAX<=20;

            CreateLabel(toggleGroup.transform, "功能开关:");
            {
                CreateToggle(toggleGroup.transform, Model.Toggle.重置筛选, (on)=>{if(on){reset_toggles();}});
            }
            reset_toggles();
        }
        public void reset_toggles(){
            ToggleDic[Model.Toggle.全部性别].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.全部生相].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.全部取向].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.全世界].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.全部立场].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.全部婚姻].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.全部归属].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.健在].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.无关紧要].GetComponent<CToggle>().isOn = true;
            ToggleDic[Model.Toggle.重置筛选].GetComponent<CToggle>().isOn = false;
            foreach(var x in InputDic.Values){
                x.text="";
            }
        }

        private void CreateMultiSearch(VerticalLayoutGroup parent)
        {
            var multiSearch = new GameObject("MultiSearch").AddComponent<HorizontalLayoutGroup>();
            SetTransform(parent.transform, multiSearch.transform);
            multiSearch.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            multiSearch.childForceExpandWidth = false;

            for (index = (int)Model.Input.角色ID; index <= (int)Model.Input.身份; ++index)
            {
                CreateLabel(multiSearch.transform, ((Model.Input)index).ToString() + ":");
                CreateInputField(multiSearch.transform, (Model.Input)index, minWidth: 150);
            }
            for (index = (int)Model.Input.特性; index <= (int)Model.Input.物品; ++index)
            {
                CreateLabel(multiSearch.transform, ((Model.Input)index).ToString() + ":");
                CreateInputField(multiSearch.transform, (Model.Input)index, minWidth: 400);
            }

            ButtonSearch = CreateButton(multiSearch.transform, Model.CharacterInfo.查找, "查找").GetComponent<CButton>();
            ButtonUpdate = CreateButton(multiSearch.transform, Model.CharacterInfo.更新数据, "更新数据").GetComponent<CButton>();
            ButtonPrevious = CreateButton(multiSearch.transform, Model.CharacterInfo.上一页, "上一页").GetComponent<CButton>();
            ButtonNext = CreateButton(multiSearch.transform, Model.CharacterInfo.下一页, "下一页").GetComponent<CButton>();
            PageCount = CreateLabel(multiSearch.transform, "").GetComponent<TextMeshProUGUI>();
        }
        #endregion

        #region InitScrollView
        private void InitScrollView()
        {
            ScrollView = ABResourceManager.Instantiate("ScrollView1");
            SetTransform(root.transform, ScrollView.transform);
            var rect = ScrollView.GetComponent<RectTransform>();
            rect.pivot = new Vector2(0, 1);
            rect.anchorMin = new Vector2(0, MARGIN);
            rect.anchorMax = new Vector2(1, 0.8f);
            rect.localScale = new Vector3(0.8f, 0.8f, 1);

            ScrollTitleContent = ScrollView.transform.Find("Title/Viewport/Content");
            var scrollTitle = ScrollView.transform.Find("Title");
            scrollTitle.gameObject.AddComponent<LayoutElement>().minHeight = 55;
            scrollTitle.GetComponent<Image>().color = new Color(0.2f, 0.2f, 0.2f, 1);
            ScrollTitleContent.gameObject.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
            ScrollTitleContent.gameObject.GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.MinSize;
            ScrollTitleContent.gameObject.AddComponent<VerticalLayoutGroup>().spacing = 20;

            ScrollDataContent = ScrollView.transform.Find("ItemList/Viewport/Content");
            var scrollData = ScrollView.transform.Find("ItemList");
            scrollData.gameObject.AddComponent<LayoutElement>().flexibleHeight = 1f;
            scrollData.GetComponent<Image>().color = new Color(0.2f, 0.2f, 0.2f, 1);
            scrollData.GetComponent<ScrollRect>().movementType = ScrollRect.MovementType.Clamped;
            scrollData.GetComponent<ScrollRect>().horizontal = false;
            scrollData.GetComponent<ScrollRect>().scrollSensitivity = 25;
            ScrollDataContent.gameObject.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
            ScrollDataContent.gameObject.GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.MinSize;
            ScrollDataContent.gameObject.AddComponent<VerticalLayoutGroup>().spacing = 20;

            CreateTitle(ScrollTitleContent);
            CreateScanResult(ScrollDataContent);
        }

        public int Width(int idx){
            if (idx == (int)Model.CharacterInfo.姓名 || idx == (int)Model.CharacterInfo.位置)
                return 200;
            else if (idx == (int)Model.CharacterInfo.前世)
                return 300;
            else if (idx >= (int)Model.CharacterInfo.人物特性 && idx <= (int)Model.CharacterInfo._END_)
                return 1000;
            else if (idx == (int)Model.CharacterInfo.周天运转)
                return 150;
            else if (idx == (int)Model.CharacterInfo.功法成长 || idx == (int)Model.CharacterInfo.技艺成长 || idx == (int)Model.CharacterInfo.状态)
                return 100;
            else if (idx == (int)Model.CharacterInfo.性别 || /*idx == (int)Model.CharacterInfo.年龄 ||*/ (idx >= (int)Model.CharacterInfo.膂力 && idx <= (int)Model.CharacterInfo.杂学))
                return 55;
            else
                return 100;
        }
        public void button(Transform trans, int idx, int width, object nul){
            CreateButton(trans, (Model.CharacterInfo)idx, ((Model.CharacterInfo)idx).ToString(), minWidth:width);
        }
        public void text(Transform trans, int idx, int width, object rowLabels){
            var label = CreateLabel(trans, "", minWidth: width).transform;
            var tmp = label.gameObject.GetComponent<TextMeshProUGUI>();
            if (idx >= (int)Model.CharacterInfo.膂力 && idx <= (int)Model.CharacterInfo.杂学)
                tmp.alignment = TextAlignmentOptions.Right;
            else if (idx == (int)Model.CharacterInfo.前世 || idx == (int)Model.CharacterInfo.人物特性)
                tmp.alignment = TextAlignmentOptions.Left;
            else if (idx == (int)Model.CharacterInfo.物品)
                tmp.alignment = TextAlignmentOptions.TopJustified;
            ((List<TextMeshProUGUI>)rowLabels).Add(tmp);
        }
        public void CreateRow(Transform trans,System.Action<Transform,int,int,object> del,object extra){
            for(int i=(int)Model.CharacterInfo.姓名;i<(int)Model.CharacterInfo._END_;i++){
                del(trans,i,Width(i),extra);
            }
        }
        private void CreateTitle(Transform parent)
        {
            var buttonTitle = new GameObject("ButtonTitle").AddComponent<HorizontalLayoutGroup>();
            buttonTitle.spacing = 20;
            SetTransform(parent, buttonTitle.transform);
            buttonTitle.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
            buttonTitle.childForceExpandWidth = false;

            CreateRow(buttonTitle.transform,this.button,null);
        }

        private void CreateScanResult(Transform parent)
        {
            ResultLines = new List<Transform>();
            ResultTMP = new List<List<TextMeshProUGUI>>();
            for (int rowIndex = 0; rowIndex < ROW_MAX; ++rowIndex)
            {
                var line = new GameObject("line" + rowIndex.ToString()).AddComponent<HorizontalLayoutGroup>();
                line.spacing = 20;
                var ltf=line.transform;
                SetTransform(parent, ltf);
                ltf.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
                line.childForceExpandWidth = false;
                line.gameObject.AddComponent<CImage>().color = new Color(0.3f, 0.3f, 0.3f, 0.5f);
                line.gameObject.AddComponent<CButton>();

                List<TextMeshProUGUI> rowLabels = new List<TextMeshProUGUI>();

                CreateRow(ltf,this.text,rowLabels);

                ResultTMP.Add(rowLabels);
                ResultLines.Add(ltf);
            }
        }
        #endregion


        #region CreateGameobject

        private GameObject CreateInputField(Transform parent, Model.Input type, string text = "", float minWidth = 100, float minHeight = 50)
        {
            var gameObject = ABResourceManager.Instantiate("InputField");
            SetTransform(parent, gameObject.transform);
            var element = gameObject.AddComponent<LayoutElement>();
            element.minWidth = minWidth;
            element.minHeight = minHeight;
            gameObject.transform.GetComponent<InputField>().text = text;
            InputDic.Add(type, gameObject.GetComponent<InputField>());

            return gameObject;
        }


        private GameObject CreateLabel(Transform parent, string text, float minWidth = 100, float minHeight = 50)
        {
            var gameObject = GameObjectCreationUtils.InstantiateUIElement(parent, "Text");
            SetTransform(gameObject.transform);
            var element = gameObject.AddComponent<LayoutElement>();
            element.minWidth = minWidth;
            element.minHeight = minHeight;
            gameObject.GetComponent<TextMeshProUGUI>().text = text;

            return gameObject;
        }

        class MyToggle {
            GameObject go;
            int start;
            int self;
            int end;
            public MyToggle(GameObject go, Model.Toggle self, Model.Toggle start, Model.Toggle end){
                this.go=go;
                this.start=(int)start;
                this.end=1+(int)end;
                this.self=(int)self;
                this.go.GetComponent<CToggle>().onValueChanged.AddListener(this.isOn);
            }
            public void isOn(bool on){
                if(on && start!=self && ToggleDic[Model.Toggle.自动反选].GetComponent<CToggle>().isOn){
                    for(int i=this.start;i<this.end;i++){
                        if(i!=this.self)ToggleDic[(Model.Toggle)i].GetComponent<CToggle>().isOn = false;
                    }
                }
            }
        }
        class FuncToggle {
            GameObject go;
            UnityEngine.Events.UnityAction<bool> toggle=null;
            public FuncToggle(GameObject go, UnityEngine.Events.UnityAction<bool> toggle=null){
                this.go=go;
                this.toggle=toggle;
                if(this.toggle is not null){
                    this.go.GetComponent<CToggle>().onValueChanged.AddListener(this.toggle);
                }
            }
        }
        private MyToggle CreateToggle(Transform parent, Model.Toggle type, Model.Toggle start=((Model.Toggle)(-1)), Model.Toggle end=((Model.Toggle)(-1)), string text=null, float minWidth = 100, float minHeight = 50)
        {
            if(text==null){
                text=type.ToString();
                start=end=type;
            }
            var gameObject = GameObjectCreationUtils.InstantiateUIElement(parent, "CommonToggle1_Switch");
            SetTransform(gameObject.transform);
            var element = gameObject.AddComponent<LayoutElement>();
            element.minWidth = minWidth;
            element.minHeight = minHeight;
            gameObject.transform.Find("Labels").Find("LabelOff").GetComponent<TextMeshProUGUI>().text = text;
            gameObject.transform.Find("Labels").Find("LabelOn").GetComponent<TextMeshProUGUI>().text = text;
            ToggleDic.Add(type, gameObject.GetComponent<CToggle>());

            return new MyToggle(gameObject,type,start,end);
        }
        private FuncToggle CreateToggle(Transform parent, Model.Toggle type, UnityEngine.Events.UnityAction<bool> isOn, string text=null, float minWidth = 100, float minHeight = 50){
            text=text??type.ToString();
            var gameObject = GameObjectCreationUtils.InstantiateUIElement(parent, "CommonToggle1_Switch");
            SetTransform(gameObject.transform);
            var element = gameObject.AddComponent<LayoutElement>();
            element.minWidth = minWidth;
            element.minHeight = minHeight;
            gameObject.transform.Find("Labels").Find("LabelOff").GetComponent<TextMeshProUGUI>().text = text;
            gameObject.transform.Find("Labels").Find("LabelOn").GetComponent<TextMeshProUGUI>().text = text;
            ToggleDic.Add(type, gameObject.GetComponent<CToggle>());

            return new FuncToggle(gameObject,isOn);
        }

        private GameObject CreateButton(Transform parent, Model.CharacterInfo type, string text, float minWidth = 100, float minHeight = 50)
        {
            var gameObject = GameObjectCreationUtils.InstantiateUIElement(parent, "BuildingQuickButton");
            SetTransform(gameObject.transform);
            gameObject.transform.Find("Text").GetComponent<TextMeshProUGUI>().text = text;
            var element = gameObject.AddComponent<LayoutElement>();
            element.minWidth = minWidth;
            element.minHeight = minHeight;
            ButtonDic.Add(type, gameObject.GetComponent<CButton>());

            return gameObject;
        }

        #endregion
        private void SetTransform(Transform parent, Transform transform)
        {
            transform.SetParent(parent, false);
            transform.localPosition = Vector3.zero;
            transform.localScale = new Vector3(1, 1, 1);
        }

        private void SetTransform(Transform transform)
        {
            transform.localPosition = Vector3.zero;
            transform.localScale = new Vector3(1, 1, 1);
        }

        public void Show()
        {
            root.SetActive(true);

            float xScale = Screen.width / InputContainer.preferredWidth;
            InputContainer.transform.localScale = new Vector3(xScale, xScale, 1);

            float viewScale=xScale * result_scale * 0.01f;

            float height = InputContainer.preferredHeight * InputContainer.transform.localScale.x;
            float yAnchor = 1 - (MARGIN * Screen.height + height) / Screen.height;
            var rect = ScrollView.GetComponent<RectTransform>();
            rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width / viewScale);
            rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Screen.height * yAnchor / viewScale);

            rect.anchorMax = new Vector2(1, yAnchor);
            ScrollView.transform.localScale = new Vector3(viewScale, viewScale, 1);

            if (blockClickGroup != null)
                blockClickGroup.interactable = false;
        }

        public void Hide()
        {
            root.SetActive(false);
            if (blockClickGroup != null)
                blockClickGroup.interactable = true;
        }
    }
}
#endif
